/* fafamtk.p FIXED ASSETS MAINTENANCE - DEPRECIATION QUERY              */
/* Copyright 1986 QAD Inc. All rights reserved.                               */
/* $Id:: fafamtk.p 79744 2017-06-23 05:58:44Z aqe                          $: */
/* REVISION: 9.1     LAST MODIFIED: 10/26/99     BY: *N021* Pat Pigatti */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00 BY: *N08T* Annasaheb Rahane */
/* REVISION: 9.1      LAST MODIFIED: 07/28/00   BY: *N0BX* Arul Victoria    */
/* REVISION: 9.1      LAST MODIFIED: 08/14/00   BY: *N0L0* Jacolyn Neder    */
/* Revision: 1.13.1.12  BY: Paul Donnelly (SB) DATE: 06/26/03 ECO: *Q00C* */
/* Revision: 1.13.1.13  BY: Robert Jensen      DATE: 09/02/04 ECO: *N2XK* */
/* Revision: 1.13.1.14  BY: Vandna Rohira      DATE: 12/17/04  ECO: *P2Z5*  */
/* REVISION: 17360     LAST MODIFIED: 05/04/12    BY: ddo                     */
/*-Revision end---------------------------------------------------------------*/
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{us/bbi/mfdeclre.i}
{us/bbi/gplabel.i} /* EXTERNAL LABEL INCLUDE */
{us/gp/gprunpdf.i "mcpl" "p"}  

define new shared temp-table temp_fabd no-undo
   field temp_yrper    like fabd_yrper
   field temp_peramt   like fabd_peramt
   field temp_accamt   like fabd_accamt
   field temp_nbv      as decimal format "->>>,>>>,>>9.99<"
   field temp_faloc_id like fabd_faloc_id
   field temp_post     like fabd_post
   index temp_yrper is unique primary temp_yrper           .

/* --------------------  Define Query  ------------------- */
define query q_temp_fabd for
   temp_fabd
   scrolling.

/* -----------------  Standard Variables  ---------------- */
define variable p-status        as character no-undo.
define variable perform-status  as character format "x(25)"
   initial "first" no-undo.
define variable p-skip-update   like mfc_logical no-undo.
define variable l-rowid         as rowid     no-undo.
define variable l-delete-it     like mfc_logical no-undo.
define variable l-del-rowid     as rowid     no-undo.
define variable l-top-rowid     as rowid     no-undo.
define variable lines           as integer   initial 10 no-undo.
define variable l-found         like mfc_logical no-undo.
define variable pos             as integer   no-undo.
define variable l-first         like mfc_logical no-undo.
define variable l-error         like mfc_logical no-undo.
define variable l-title         as character no-undo.

define variable l_amt_fmt       as character no-undo.
define variable mc-error-number like msg_nbr no-undo.
define variable rndmthd         like rnd_rnd_mthd no-undo.

/* ------------------  Button Variables  ----------------- */
define button b-detail label "Detail".
define button b-uop label "UOP".
define button b-end label "End".

/* -------------  Standard Widget Variables  ------------- */
define variable l-focus   as widget-handle no-undo.
define variable w-frame   as widget-handle no-undo.

/* ------------------  Screen Variables  ----------------- */
define shared variable l-fa-id   like fabd_fa_id no-undo.
define shared variable l-fabk-id like fabd_fabk_id no-undo.
define variable l-curr    like currency.currencycode no-undo.
define variable l-init    like mfc_logical
   initial yes no-undo.

/* ------------------  Frame Definition  ----------------- */
define frame f-button
   b-detail at 1
   b-uop at 10
   b-end at 19
with no-box overlay three-d side-labels width 28.

/* CLEAR FRAME REGISTRATION TO TRANSLATE THE FRAME EVERYTIME*/
clearFrameRegistration(frame f-button:handle).

/* SET EXTERNAL LABELS */
setFrameLabels(frame f-button:handle).

assign
   l-focus               = b-detail:handle in frame f-button
   b-detail:width        = 8
   b-detail:private-data = "detail"
   b-uop:width           = 8
   b-uop:private-data    = "uop"
   b-end:width           = 8
   b-end:private-data    = "end".

define frame f-h
   skip(.4)
   l-fa-id colon 10
   l-fabk-id colon 30
   l-curr colon 50
   with three-d overlay side-labels width 80.

/* CLEAR FRAME REGISTRATION TO TRANSLATE THE FRAME EVERYTIME*/
clearFrameRegistration(frame f-h:handle).

/* SET EXTERNAL LABELS */
setFrameLabels(frame f-h:handle).

define frame f-1
   skip(.4)
   temp_yrper
   temp_faloc_id
   temp_peramt
   temp_accamt column-label "Acc Depreciation"
   temp_nbv    column-label "Net Book Value"
   temp_post
with three-d overlay 8 down scroll 1 width 80.

/* CLEAR FRAME REGISTRATION TO TRANSLATE THE FRAME EVERYTIME*/
clearFrameRegistration(frame f-1:handle).

/* SET EXTERNAL LABELS */
setFrameLabels(frame f-1:handle).

run ip-framesetup.

w-frame = frame f-1:handle.
{us/bbi/gprun.i ""fafmtut.p"" "(w-frame)"}

lines = 8.

for first fabk_mstr 
   where fabk_mstr.fabk_domain = global_domain 
     and fabk_mstr.fabk_id = l-fabk-id 
no-lock:
   for first cu_mstr where cu_mstr.oid_cu_mstr = fabk_mstr.oid_cu_mstr no-lock:
      l-curr = cu_mstr.cu_curr.
   end.
end.

/* ---------------  Pre Processing Include  -------------- */
display
   l-fa-id
   l-fabk-id
   l-curr
   with frame f-h.

{us/bbi/gprun.i ""fadpblc.p"" 
   "(input l-fa-id,
     input l-fabk-id)"}
/* -------------  END Pre Processing Include  ------------ */

open query q_temp_fabd for each temp_fabd no-lock.

get first q_temp_fabd no-lock.


l_amt_fmt = temp_peramt:format in frame f-1.

main-loop:
do while perform-status <> "end" on error undo:

   if {us/bbi/gpiswrap.i}
   then
      frame f-1:visible = yes.

   run ip-query
      (input-output perform-status,
       input-output l-rowid).

   if {us/bbi/gpiswrap.i}
   then
      hide frame f-1 no-pause.

   /* ----------------------  Detail  --------------------- */
   if perform-status = "detail"
   then do:
      p-status = "display".
      if session:display-type = "gui"
      then
         assign
            temp_yrper:bgcolor = 8
            temp_yrper:fgcolor = 0.
      {us/bbi/gprun.i ""fafamtp.p""}
      assign
         p-status       = ""
         perform-status = "first".
      run ip-displayedits.
   end.  /* IF perform-status = "detail"  */
   /* --------------------  End Detail  ------------------- */

   /* -----------------------  UOP  ----------------------- */
   if perform-status = "uop"
   then do:
      p-status = "display".
      if session:display-type = "gui"
      then
         assign
            temp_yrper:bgcolor = 8
            temp_yrper:fgcolor = 0.
      {us/bbi/gprun.i ""fafamte.p""}
      assign
         p-status       = ""
         perform-status = "first".

      run ip-displayedits.
   end.  /* IF perform-status = "uop"  */
   /* ---------------------  End UOP  --------------------- */

   /* ----------------------  Display  ---------------------- */
   if {us/bbi/gpiswrap.i}
   then
      frame f-1:visible = yes.

   run ip-predisplay.

   /* -----------------------  End  ----------------------- */
   if perform-status = "end"
   then do:
      hide frame f-1 no-pause.
      delete PROCEDURE this-procedure no-error.
      leave.
   end.  /* IF perform-status = "end"  */

   /* ----------------  Selection Buttons  ---------------- */
   if perform-status <> "first"
   then
      run ip-button
         (input-output perform-status).

   if perform-status = "uop"
   then do:
      for first fab_det
         where fab_det.fab_domain = global_domain
           and fab_fa_id = l-fa-id 
           and fab_fabk_id = l-fabk-id
      no-lock:
         if not can-find(first famt_mstr where 
            famt_mstr.famt_domain = global_domain and
            famt_id = fab_famt_id and
            famt_type = "2")
         then do:
            /* UOP OPTION ONLY AVAILABLE FOR UOP METHODS */
            {us/bbi/pxmsg.i &MSGNUM=3200 &ERRORLEVEL=4}
            perform-status = "first".
         end.  /* IF NOT CAN-FIND(first famt_mstr */
      end.  /* FOR FIRST fab_det */
   end.  /* IF perform-status = "uop"  */

end.  /* MAIN-LOOP */

PROCEDURE ip-predisplay:
   if (perform-status = "" or
      perform-status = "update") and
      available temp_fabd
   then
   display-loop:
   do:
      if perform-status = ""
      then do:
         clear frame f-1 all no-pause.
         l-top-rowid = rowid(temp_fabd).
      end.  /* IF perform-status = ""  */
      read-loop:
      do pos = 1 to lines:
         if perform-status = ""
         then do:
            if session:display-type = "gui"
            then
               assign
                  temp_yrper:bgcolor = 8
                  temp_yrper:fgcolor = 0.
            else
               color display normal temp_yrper
                  with frame f-1.
         end.  /* IF perform-status = ""  */
         run ip-displayedits.
         if perform-status = "update"
         then
            leave display-loop.
         if pos < lines
         then
            down with frame f-1.
         get next q_temp_fabd no-lock.
         if not available temp_fabd
         then
            leave.
      end.  /* READ-LOOP */
      run ip-postdisplay.
   end.  /* DISPLAY-LOOP */
END PROCEDURE. /* ip-predisplay */

PROCEDURE ip-displayedits:
   if available temp_fabd
   then do:

      /* DISPLAY-EDITS */
      if l-init = yes
      then do:
         for last fabd_det
            where fabd_det.fabd_domain = global_domain and
                  fabd_fa_id = l-fa-id and
                  fabd_fabk_id = l-fabk-id and
                  fabd_post = yes
         no-lock
         use-index fabd_fa_id:
            for first temp_fabd
               where temp_yrper = fabd_yrper
            no-lock:        
               l-rowid = rowid(temp_fabd).
               run ip-query(input-output perform-status,
                            input-output l-rowid).
               perform-status = "first".
            end.  /* FOR FIRST temp_fabd */
         end.  /* FOR LAST fabd_det */
         l-init = no.
      end.  /* IF l-init = yes */

      /* DISPLAY-EDITS */

      {us/gp/gprunp.i "mcpl" "p" "mc-get-rnd-mthd"
                "(input l-curr,
                  output rndmthd,
                  output mc-error-number)"}
      if mc-error-number <> 0 
      then do:
         {us/bbi/pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=3}
      end.

      {us/bbi/gprun.i ""gpcurfmt.p""
                "(input-output l_amt_fmt, input rndmthd)"}

      assign
         temp_fabd.temp_peramt:format in frame f-1 = l_amt_fmt
         temp_fabd.temp_accamt:format in frame f-1 = l_amt_fmt
         temp_fabd.temp_nbv   :format in frame f-1 = l_amt_fmt.

      run ip-display.
   end.  /* IF AVAILABLE temp_fabd  */
END PROCEDURE. /* ip-displayedits */

PROCEDURE ip-display:
   display
      l-fa-id
      l-fabk-id
      l-curr
      with frame f-h.
   display
      temp_fabd.temp_yrper
      temp_fabd.temp_nbv
      temp_fabd.temp_peramt
      temp_fabd.temp_faloc_id
      temp_fabd.temp_accamt
      temp_fabd.temp_post  
      with frame f-1.
END PROCEDURE. /* ip-display */

PROCEDURE ip-postdisplay:
   do pos = 1 to lines:
      if frame-line(f-1) <= 1
      then
         leave.
      up 1 with frame f-1.
   end.  /* DO pos = 1 TO LINES */
   if perform-status = ""
   then do:
      reposition q_temp_fabd to rowid l-top-rowid no-error.
      get next q_temp_fabd no-lock.
      l-rowid = l-top-rowid.
      color display message temp_yrper
         with frame f-1.
   end.  /* IF perform-status = ""  */
END PROCEDURE. /* ip-postdisplay */

PROCEDURE ip-query:
   define input-output parameter perform-status as character no-undo.
   define input-output parameter l-rowid as rowid no-undo.

   if perform-status = "first"
   then do:
      if l-rowid <> ?
      then do:
         reposition q_temp_fabd to rowid l-rowid no-error.
         get next q_temp_fabd no-lock.
      end.  /* IF l-rowid <> ?  */
      if not available temp_fabd
      then
         get first q_temp_fabd no-lock.
      if available temp_fabd
      then
         assign
            perform-status = ""
            l-rowid        = rowid(temp_fabd).
      else do:
         assign
            perform-status = ""
            l-rowid        = ?.
         assign
            frame f-1:visible = true.
         return.
      end.  /* IF AVAILABLE temp_fabd  */
   end.  /* IF perform-status = "first"  */
   if perform-status = "open"
   then do:
      open query q_temp_fabd for each temp_fabd no-lock.

      reposition q_temp_fabd to rowid l-rowid no-error.
      get next q_temp_fabd no-lock.
      if available temp_fabd
      then
         assign
            perform-status = ""
            l-rowid        = rowid(temp_fabd).
      else do:
         get first q_temp_fabd no-lock.
         if not available temp_fabd
         then do:
            assign
               perform-status    = ""
               frame f-1:visible = true.
            return.
         end.  /* IF NOT AVAILABLE temp_fabd  */
         else
            assign
               perform-status = ""
               l-rowid        = rowid(temp_fabd).
      end.  /* IF NOT AVAILABLE temp_fabd  */
   end.  /* IF perform-status = "open"  */
   if perform-status = "next"
   then do:
      get next q_temp_fabd no-lock.
      if available temp_fabd
      then do:
         hide message no-pause.
         assign
            l-rowid        = rowid(temp_fabd)
            perform-status = "next".
         if session:display-type = "gui"
         then
            assign
               temp_yrper:bgcolor = 8
               temp_yrper:fgcolor = 0.
         else
            color display normal temp_yrper
               with frame f-1.
         pause 0. /* MAKES SCROLLING WORK - DON'T REMOVE */
         down 1 with frame f-1.
         run ip-displayedits.
         color display message temp_yrper
            with frame f-1.
      end.  /* IF AVAILABLE temp_fabd  */
      else do:
         perform-status = "next".
         {us/bbi/pxmsg.i &MSGNUM=20 &ERRORLEVEL=2} /* END OF FILE */
         get last q_temp_fabd no-lock.
      end.  /* IF NOT AVAILABLE temp_fabd  */
   end.  /* IF perform-status = "next"  */
   if perform-status = "prev"
   then do:
      get prev q_temp_fabd no-lock.
      if not available temp_fabd
      then do:
         {us/bbi/pxmsg.i &MSGNUM=21 &ERRORLEVEL=2} /* BEGINNING OF FILE */
         get first q_temp_fabd no-lock.
         return.
      end.  /* IF NOT AVAILABLE temp_fabd  */
      hide message no-pause.
      l-rowid = rowid(temp_fabd).
      if session:display-type = "gui"
      then
         assign
            temp_yrper:bgcolor = 8
            temp_yrper:fgcolor = 0.
      else
         color display normal temp_yrper
            with frame f-1.
      pause 0.  /* MAKES SCROLLING WORK - DON'T REMOVE */
      up 1 with frame f-1.
      run ip-displayedits.
      color display message temp_yrper
         with frame f-1.
   end.  /* IF perform-status = "prev"  */
   if perform-status = "update" or
      perform-status = "delete"
   then do:
      get current q_temp_fabd no-lock.
      if not available temp_fabd
      then do:
         hide message no-pause.
         {us/bbi/pxmsg.i &MSGNUM=1310 &ERRORLEVEL=3} /* RECORD NOT FOUND */
         assign
            perform-status = "".
      end.  /* IF NOT AVAILABLE temp_fabd  */
   end.  /* IF perform-status = "update" OR ... */
END PROCEDURE. /* ip-query */

PROCEDURE ip-framesetup:
   if session:display-type = "gui"
   then
      assign
         frame f-h:row = 3
         frame f-1:row = 4.5.
   else
      assign
         frame f-h:row = 2
         frame f-1:row = 5.

   assign
      frame f-h:box           = true
      frame f-h:centered      = true
      frame f-1:box           = true
      frame f-1:centered      = true
      frame f-1:title         = (getFrameTitle("DEPRECIATION_QUERY",26))
      frame f-button:centered = true
      frame f-button:row      = 20.
END PROCEDURE. /* ip-framesetup */

PROCEDURE ip-button:
   define input-output parameter perform-status as character format "x(25)" no-undo.
   enable all with frame f-button.
   assign
      ststatus = stline[1].
   status input ststatus.

   on choose of b-detail
      do:
         assign
            perform-status = self:private-data
            l-focus        = self:handle
            l-title        = current-window:title
                           + " - "
                           + self:label.
         hide frame f-button no-pause.
      end.  /* ON CHOOSE OF b-detail */

   on choose of b-uop
      do:
         assign
            perform-status = self:private-data
            l-focus        = self:handle
            l-title        = current-window:title
                           + " - "
                           + self:label.
         hide frame f-button no-pause.
      end.  /* ON CHOOSE OF b-uop */

   on choose of b-end
      do:
         assign
            perform-status = self:private-data
            l-focus        = self:handle.
         hide frame f-h no-pause.
         hide frame f-1 no-pause.
         hide frame f-button no-pause.
      end.  /* ON CHOOSE OF b-end */

   on cursor-up, f9 anywhere
      do:
         assign
            perform-status = "prev"
            l-focus        = focus:handle.
         apply "go" to frame f-button.
      end.  /* ON CURSOR-UP, F9 ANYWHERE */

   on cursor-down, f10 anywhere
      do:
         assign
            perform-status = "next"
            l-focus        = focus:handle.
         apply "go" to frame f-button.
      end.  /* ON CURSOR-DOWN, F10 ANYWHERE */

   on cursor-right anywhere
      do:
         l-focus = self:handle.
         apply "tab" to l-focus.
      end.  /* ON CURSOR-RIGHT ANYWHERE */

   on cursor-left anywhere
      do:
         l-focus = self:handle.
         if session:display-type = "gui"
         then
            apply "shift-tab" to l-focus.
         else
            apply "ctrl-u" to l-focus.
      end.  /* ON CURSOR-LEFT ANYWHERE */

   on esc anywhere
      do:
         if session:display-type = "gui"
         then
            apply "choose" to b-end in frame f-button.
      end.  /* ON ESC ANYWHERE */

   on end-error anywhere
      apply "choose" to b-end in frame f-button.

   on window-close of current-window
      apply "choose" to b-end in frame f-button.

   on go anywhere
      do:
         if (lastkey = keycode("cursor-down") or
            lastkey = keycode("cursor-up") or
            lastkey = keycode("f9") or
            lastkey = keycode("f10"))
         then
            return.
         else
            assign
               l-focus = focus:handle.
         apply "choose" to l-focus.
      end.  /* ON GO ANYWHERE */

   wait-for
      go of frame f-button or
      window-close of current-window or
      choose of
      b-detail,
      b-uop,
      b-end
      focus
      l-focus.
   hide message no-pause.
END PROCEDURE. /* ip-button */
